package com.justgame.project.service.just.query;

import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.justgame.project.cloud.common.query.Column;
import com.justgame.project.cloud.common.query.BaseSelectBuilder;
import com.justgame.project.cloud.common.query.AbstractQueryWrapper;
import com.justgame.project.cloud.common.query.Query;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.justgame.project.service.just.entity.JustGameTypeMap;
import com.justgame.project.service.just.query.JustGameInfoQuery;
import com.justgame.project.service.just.entity.JustGameInfo;
import com.justgame.project.service.just.entity.JustGameType;
import com.justgame.project.service.just.query.JustGameTypeQuery;
import com.justgame.project.service.just.wrapper.JustGameTypeMapWrapper;

public class JustGameTypeMapQuery extends AbstractQueryWrapper<JustGameTypeMap, MPJLambdaWrapper<JustGameTypeMap>, JustGameTypeMapWrapper> {

    private JustGameTypeMapQuery() {
        super(new MPJLambdaWrapper<>(JustGameTypeMap.class,"just_game_type_map"));
    }

    public static JustGameTypeMapQuery builder(){
        return new JustGameTypeMapQuery();
    }

    public static class Select{

        private final List<Query> queries;

        protected Select(){
            queries = new ArrayList<>();
        }

        protected Select(List<Query> queries) {
            this.queries = queries;
        }

        public Query[] getQueriesArray() {
            return queries.toArray(new Query[0]);
        }

        public static SelectBuilder builder(){
            return new SelectBuilder();
        }

        public Query[] build(){
            return queries.toArray(new Query[0]);
        }

        public static class SelectBuilder extends BaseSelectBuilder {
            public SelectBuilder JustGameTypeMap(){
                return JustGameTypeMap(Column.all());
            }

            public SelectBuilder JustGameTypeMap(Column column){
                return add(Query.builder(JustGameTypeMap.class)
                            .selectColumns(column)
                            .build());
            }

            public SelectBuilder justGameInfo(){
                return justGameInfo(Column.all());
            }

            public SelectBuilder justGameInfo(Column column){
                return justGameInfo(column, null);
            }

            public SelectBuilder justGameInfo(Column column,JustGameInfoQuery.Select selectQuery){
                return justGameInfo("justGameInfoWrapper", column, null);
            }

            public SelectBuilder justGameInfo(String mappingName,Column column,JustGameInfoQuery.Select selectQuery){
                return add(Query.builder(JustGameInfo.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<JustGameInfo,?>)JustGameInfo::getId)
                            .rightOn((SFunction<JustGameTypeMap,?>)JustGameTypeMap::getGameInfoId)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(JustGameInfoQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public SelectBuilder justGameType(){
                return justGameType(Column.all());
            }

            public SelectBuilder justGameType(Column column){
                return justGameType(column, null);
            }

            public SelectBuilder justGameType(Column column,JustGameTypeQuery.Select selectQuery){
                return justGameType("justGameTypeWrapper", column, null);
            }

            public SelectBuilder justGameType(String mappingName,Column column,JustGameTypeQuery.Select selectQuery){
                return add(Query.builder(JustGameType.class)
                            .mapping(mappingName)
                            .leftOn((SFunction<JustGameType,?>)JustGameType::getId)
                            .rightOn((SFunction<JustGameTypeMap,?>)JustGameTypeMap::getTypeId)
                            .selectColumns(column)
                            .join(Optional.ofNullable(selectQuery).map(JustGameTypeQuery.Select::getQueriesArray).orElse(null))
                            .build());
            }

            public Select build(){
                return new Select(getQueries());
            }
        }

    }
}